var express = require('express');
var router = express.Router();
const { User, Student, Class, Dormitory, Attendance, DisciplinaryRecord, AuditLog, Department } = require("../models/db");

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

// 用户User相关路由
router.get("/user", async (req, res) => {
  try {
    const users = await User.find().populate("department");
    res.json(users);
  } catch (error) {
    res.status(500).json({ message: "服务器错误" });
  }
});

// 学生Student相关路由
router.get("/student", async (req, res) => {
  try {
    const students = await Student.find().populate("class");
    res.json(students);
  } catch (error) {
    res.status(500).json({ message: "服务器错误" });
  }
});

// 班级Class相关路由
router.get("/class", async (req, res) => {
  try {
    const classes = await Class.find().populate("teacher");
    res.json(classes);
  } catch (error) {
    res.status(500).json({ message: "服务器错误" });
  }
});

// 宿舍Dormitory相关路由
router.get("/dormitory", async (req, res) => {
  try {
    const dormitories = await Dormitory.find();
    res.json(dormitories);
  } catch (error) {
    res.status(500).json({ message: "服务器错误" });
  }
});

// 考勤Attendance相关路由
router.get("/attendance", async (req, res) => {
  try {
    const attendances = await Attendance.find().populate("student").populate("class");
    res.json(attendances);
  } catch (error) {
    res.status(500).json({ message: "服务器错误" });
  }
});

// 违纪DisciplinaryRecord相关路由
router.get("/disciplinaryrecord", async (req, res) => {
  try {
    const records = await DisciplinaryRecord.find().populate("student");
    res.json(records);
  } catch (error) {
    res.status(500).json({ message: "服务器错误" });
  }
});

// 审核日志AuditLog相关路由
router.get("/auditlog", async (req, res) => {
  try {
    const logs = await AuditLog.find().populate("user");
    res.json(logs);
  } catch (error) {
    res.status(500).json({ message: "服务器错误" });
  }
});

// 学院Department相关路由
router.get("/department", async (req, res) => {
  try {
    const departments = await Department.find().populate("head").populate("faculty");
    res.json(departments);
  } catch (error) {
    res.status(500).json({ message: "服务器错误" });
  }
});


































module.exports = router;
